Lambda expressions are an alternative way of defining functions.
So, suppose that we have this function definition and here I'm using args,
as a stand-in for a list of arguments,
so I might have x, y, z.
These arguments might even have default values,
I might have x equals one etc.,
and we have some return value expression.
So, that might be return x plus five or any expression that we want to return.
If we have that kind of function,
then we can express it using a Lambda expression.
This is a Lambda expression.
Whatever we wrote for our list of arguments here,
we could transplant to our Lambda expression and reuse it down here.
So, we could have Lambda,
and then our argument might be x and y,
and then we say colon and then whatever value we want to return,
we can again specify in our Lambda.
So, we might say return x plus y.
This expression, has a value whose type is a function.
We don't even need to give this function a name,
like we do when we use def.
If we do want to give it a name,
we can say func equals and then provide our Lambda function,
but we can also make it an anonymous function,
in other words, a function that doesn't have a name.
So, let's look at some code examples.
Here on line one,
we define a function f,
it takes in one argument x and it returns x minus one.
Again, this is kind of the traditional way of defining a function.
If we called our function by saying print f called with three,
then we should get the value two.
If we print out the function object itself by saying print f,
then we should get that this is a function named f,
and if we print out the type of f by saying print the type of f,
then we should get that f is a function.
Now, if we wanted to write this function with an equivalent Lambda expression,
then we would say Lambda x,
which is our argument, and then we went to return x minus one,
so we just write x minus one.
Again, we don't need to add a return statement for Lambda expressions.
In Lambda, the return is implicit.
So, if we print out the value of this expression,
then we should see that it's a function,
so in this case, it's a Lambda function specifically.
If I instead say lf equals this value,
and if I print out lf called with three,
then we should get two.
If I print out the type of this Lambda function,
then we should see that just like f,
our Lambda function lf is a function.
So suppose that we wanted to convert this traditional function definition,
called last char, into a Lambda function,
then what we could do is we could say last char equals a Lambda,
it takes in one argument s,
so Lambda s colon,
and then again remember the return is implicit with Lambda,
so we can just say s sub negative one.
Now, the value of this expression is a function,
which is equivalent to this function.
We don't necessarily need to actually give it a name,
but here we chose to give it the name last char.
So, let's answer some multiple choice questions about Lambda functions.
So, this question asks if the input to
this Lambda function is a number, then what's returned?
So, here we have a Lambda expression,
it takes in one argument x,
and it returns negative x.
So, what that tells me,
is that if we pass in one,
we should get negative one,
if we pass in negative 10,
we should get positive 10,
etc., and so that to me is B,
a number of the opposite sign so
positive numbers become negative negative numbers become positive.
So, i would answer B.
That's all for now, until next time.